package com.longqian.controller;

import com.longqian.common.BaseResponse;
import com.longqian.common.ResultUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.longqian.common.AliOssUtil;
import java.io.IOException;
import java.util.UUID;

/**
 * 文件操作
 *
 */

@RestController
@Slf4j
@RequestMapping("/fileOss")
@CrossOrigin(origins = {"http://localhost:5173"}, allowCredentials = "true")
@ApiOperation(value = "文件传输")
public class FileController {
    @Autowired//依赖注入，什么玩意来着
    private AliOssUtil aliOssUtil;
    /**
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public BaseResponse<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("文件上传，{}",file);
            String originalFilename = file.getOriginalFilename();//原始文件名
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName =UUID.randomUUID().toString()+extension;
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return ResultUtils.success(filePath);

    }
}
